package com.gzw.dms.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.gzw.dms.R;
import com.gzw.dms.bean.BackOrderInfo;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/5/23.
 */
public class BackQSAdapter extends BaseAdapter {
    private Context mContext;
    private List<BackOrderInfo> lsInfo;
    private static HashMap<Integer, Boolean> isSelected;

    public BackQSAdapter(Context _mContext,
                            List<BackOrderInfo> _lsInfo) {
        this.mContext = _mContext;
        this.lsInfo = _lsInfo;

        isSelected = new HashMap<Integer, Boolean>();
        // 初始化数据
        initDate();
    }



    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < lsInfo.size(); i++) {
            getIsSelected().put(i, false);
        }
    }

    @Override
    public View getView(final int arg0, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub
        // 加载布局文件
        final  Holder mHolder;

        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_back_adapter, null);
            mHolder = new Holder();
            mHolder.ic_send_mechanism = (TextView) convertView.findViewById(R.id.ic_send_mechanism).findViewById(R.id.tv_name);
            mHolder.ic_time_limitation = (TextView) convertView.findViewById(R.id.ic_time_limitation).findViewById(R.id.tv_name);
            mHolder.ic_custom_name = (TextView) convertView.findViewById(R.id.ic_custom_name).findViewById(R.id.tv_name);
            mHolder.ic_goods_num = (TextView) convertView.findViewById(R.id.ic_goods_num).findViewById(R.id.tv_name);
            mHolder. ic_remarks = (TextView) convertView.findViewById(R.id.ic_remarks).findViewById(R.id.tv_name);
            mHolder.ic_end_address = (TextView) convertView.findViewById(R.id.ic_end_address).findViewById(R.id.tv_name);
            mHolder.ic_type = (TextView) convertView.findViewById(R.id.ic_type).findViewById(R.id.tv_name);
            mHolder.ic_updatetime = (TextView) convertView.findViewById(R.id.ic_updatetime).findViewById(R.id.tv_name);
            mHolder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);
            mHolder.ic_custom_tel = (TextView) convertView.findViewById(R.id.ic_custom_tel).findViewById(R.id.tv_name);

            convertView.setTag(mHolder);
        } else {
            mHolder = (Holder) convertView.getTag();
        }

        BackOrderInfo shippingOrderInfo = lsInfo.get(arg0);
        mHolder.ic_send_mechanism.setText("货运编号:"+shippingOrderInfo.getShiping_order_num());
        mHolder.ic_time_limitation.setText("受理机构:"+shippingOrderInfo.getSend_mechanism());
        mHolder.ic_custom_name.setText("调拨/退货终到位置:"+shippingOrderInfo.getEnd_mechanism());
//        mHolder.ic_custom_tel.setText("运单状态:"+shippingOrderInfo.getCustom_tel());
        mHolder.ic_goods_num.setText("总件数:"+shippingOrderInfo.getGoods_num());
        mHolder.ic_remarks.setText("项目部备注:"+shippingOrderInfo.getRemarks());
        mHolder.ic_end_address.setText("调拨/退货始发位置:"+shippingOrderInfo.getEnd_address());
        mHolder.ic_updatetime.setText("起运时间:" + shippingOrderInfo.getSend_time());
        int n = shippingOrderInfo.getShipping_order_state();

        if (n==0){
            mHolder.ic_custom_tel.setText("运单状态:"+""+"已发运");

        }else if (n==2){
            mHolder.ic_custom_tel.setText("运单状态:"+""+"已接单");

        }else if (n==4){
            mHolder.ic_custom_tel.setText("运单状态:"+""+"已签收");

        }
        int i = shippingOrderInfo.getOrder_type();
        if (i==1){
            mHolder.ic_type.setText("运单类型:"+""+"调拨");

        }else {
            mHolder.ic_type.setText("运单类型:"+""+"退货");

        }
        mHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                mHolder.cb.setChecked(isChecked);
//                isSelected.put(arg0, false);

                //先把链表中的数据取代掉
                isSelected.put(arg0, isChecked);
                if(buttonView.isChecked())
                {
                    for(int i=0;i<lsInfo.size();i++)
                    {
                        //把其他的checkbox设置为false
                        if(i!=arg0){
                            isSelected.put(i, false);
                        }
                    }
                }
                //通知适配器更改
                BackQSAdapter.this.notifyDataSetChanged();

            }

        });
        mHolder.cb.setChecked(getIsSelected().get(arg0));
        return convertView;
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        BackQSAdapter.isSelected = isSelected;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return lsInfo.get(arg0);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return lsInfo.size();
    }

    public static class Holder {
        public TextView ic_send_mechanism;
        public TextView ic_time_limitation;
        public TextView ic_custom_name;
        public TextView ic_goods_num;
        public TextView ic_remarks;
        public TextView ic_end_address;
        public TextView ic_updatetime;
        public TextView ic_custom_tel;
        public TextView ic_type;
        public TextView ic_shipping_order_state;
        public CheckBox cb;
    }
}

